package net.lwf.web.tags;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

import net.lwf.entity.WebUser;
import net.lwf.number.util.RandomNumberGenerator;
import net.lwf.web.struts.action.UserDataHelper;

public class GradeStyleTag extends TagSupport {
	
	private static final long serialVersionUID = 1L;

	private String[] gradebg 
		= {"FFFF66", "CCFF66", "FFCCFF", "FFCC66", "99FF00", "66FFCC", "6666FF", "FFCC99", "FF9966", "FF99FF", "CCFF33", "CC99FF", "99CCCC", "66CCFF", "FF6666", "CCCCCC"};

	private String grade;
	
	public int doStartTag() throws JspException {
		try {
			pageContext.getOut().print(this.getStyle());
		} catch (Exception ex) {
			throw new JspTagException("SimpleTag: " + ex.getMessage());
		}
		return SKIP_BODY;
	}

	public int doEndTag() {
		return EVAL_PAGE;
	}
		
	public String getGrade() {
		return grade;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}

	private String getStyle() {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		String style = "";
		int colorchoice = RandomNumberGenerator.generate(1, gradebg.length);
		WebUser webuser = UserDataHelper.getWebUser(request);
		if(webuser != null) {
			if(webuser.getUser().getGrade().equals(grade)) {
				style += "style=\"border:solid 2px #F87431" + "\"";
			}
		}
		style += " bgcolor=\"#" + gradebg[colorchoice-1] + "\"";

		return style;
	}

}
